.var ACT_LENGTH = $12
.var SHOW_SPR_X = $14
.var SHOW_SPR_Y = $16
.var CARRY_X_MASKS = $18
.var SPR_INDEX = $1a

.macro resetSpriteEngine()
{
	lda #$00
	sta SPR_ENABLE
	sta SPR_INDEX
	sta SPR_X_CARRY
	lda #$01
	sta CARRY_X_MASKS
}

.macro showNextSprite()
{
	lda SPR_ENABLE
	asl
	clc
	adc #$01
	sta SPR_ENABLE
	sta SPR_MULTICOLOR_MODE
	sta SPR_STRECH_Y
	
	//set address
	ldx SPR_INDEX
	lda (THIS),Y
	sta SPR0_ADDR,X
	
	//set color
	iny
	lda (THIS),Y
	sta SPR0_COLOR,X
	
	//set x coord
	iny
	lda (THIS),Y
	sta NUM1
	:UpscaleSignedNum(NUM1,1,2)
	:Add(NUM1,SHOW_SPR_X,NUM2,2)
	
	lda #$00
	cmp NUM2+1
	beq carry_x_skip2
	lda SPR_X_CARRY
	ora CARRY_X_MASKS
	sta SPR_X_CARRY
carry_x_skip2:
	
	lda SPR_INDEX
	clc
	adc SPR_INDEX
	tax
	
	lda NUM2 
	sta SPR0_X,X
	
	//set y coord
	iny
	lda (THIS),Y
	sta NUM1
	clc 
	adc SHOW_SPR_Y
	inx
	sta SPR0_X,X
	
	inc SPR_INDEX
	lda CARRY_X_MASKS
	asl
	sta CARRY_X_MASKS
	iny
}

show_complex_sprite:
	ldy #$00
	lda (THIS),Y
	sta ACT_LENGTH
	iny
show_complex_sprite2_loop:
	lda ACT_LENGTH
	cmp #$00
	beq show_complex_sprite2_loop_end
	:showNextSprite()
	dec ACT_LENGTH
	jmp show_complex_sprite2_loop
show_complex_sprite2_loop_end:
	rts


.macro showSpriteA(thisaddr,xaddr,yaddr)
{
	:copy(xaddr,SHOW_SPR_X,2)
	:copy(yaddr,SHOW_SPR_Y,1)
	:copy(thisaddr,THIS,2)
	jsr show_complex_sprite
}

.var ACT_FPC = $40
.var ACT_ANL = $41
.var TEMP_THIS = $42

play_animation:
 
    ldy #$00 //frame / count
    lda (THIS),Y
    sta ACT_FPC
    iny //counter
    lda (THIS),Y
    cmp ACT_FPC
    beq next_anim_phase

    lda (THIS),Y //inc counter
    clc
    adc #$01
    sta (THIS),Y

    iny //anim length
    iny //anim phase
    lda (THIS),Y
    jmp next_anim_phase_end
next_anim_phase:
    lda #00
    sta (THIS),Y //reset counter to 0
    iny //anim length
    lda (THIS),Y
    sta ACT_ANL
    iny //anim phase
    
    lda (THIS),Y //inc anim pahse
    clc
    adc #$01
    sta (THIS),Y    

    cmp ACT_ANL
    beq reset_anim_phase
    jmp next_anim_phase_end
reset_anim_phase:
    lda #$00
    sta (THIS),Y
next_anim_phase_end:  

//get address of complex frame
//in a reg anim pahse

    ldx #$00
    sta ACT_ANL

find_anim_frame: 
    cpx ACT_ANL
    beq find_anim_frame_end
    iny
    iny
    inx
    jmp find_anim_frame
find_anim_frame_end:
    
    iny 
    lda (THIS),Y
    sta TEMP_THIS
    iny
    lda (THIS),Y
    sta TEMP_THIS+1

    lda TEMP_THIS
    sta THIS
    lda TEMP_THIS+1
    sta THIS+1

    jsr show_complex_sprite

    rts //play_animation

.macro playAnimation(label,x,y)
{
	lda #<x 
	sta SHOW_SPR_X
	lda #>x
	sta SHOW_SPR_X+1
	lda #y
	sta SHOW_SPR_Y
	:setThis(label)
	jsr play_animation
}

.macro playAnimationA(thisaddr,xaddr,yaddr)
{
	:copy(xaddr,SHOW_SPR_X,2)
	:copy(yaddr,SHOW_SPR_Y,1)
	:copy(thisaddr,THIS,2)
	jsr play_animation
}
